package com.lanxiao.cloud.modules.oauth.application;

import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.service.IOperatorService;
import com.lanxiao.cloud.common.core.utils.PageUtils;
import com.lanxiao.cloud.common.web.service.ILog;
import com.lanxiao.cloud.modules.oauth.application.assembler.OauthAssembler;
import com.lanxiao.cloud.modules.oauth.application.dto.LoginCmd;
import com.lanxiao.cloud.modules.oauth.application.dto.LoginUserInfoDto;
import com.lanxiao.cloud.modules.oauth.application.dto.UserDetailsDto;
import com.lanxiao.cloud.modules.oauth.domain.factory.LoginFactory;
import com.lanxiao.cloud.modules.oauth.domain.model.UserAuth;
import com.lanxiao.cloud.modules.oauth.domain.model.UserDetails;
import com.lanxiao.cloud.modules.oauth.domain.service.ISecurityService;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.springframework.stereotype.Component;

/**
 * @author ：qiaoliang
 * @date ：2021-09-10
 */
@Component
public class OauthQueryManager {

  private final LoginFactory loginFactory;
  private final IOperatorService operatorService;
  private final ISecurityService securityService;
  private final OauthAssembler oauthAssembler;

  public OauthQueryManager(
      LoginFactory loginFactory,
      IOperatorService operatorService,
      ISecurityService securityService,
      OauthAssembler oauthAssembler) {
    this.loginFactory = loginFactory;
    this.operatorService = operatorService;
    this.securityService = securityService;
    this.oauthAssembler = oauthAssembler;
  }

  /**
   *  登录
   *
   * @author ：qiaoliang
   * @param loginQuery : 登录信息
   * @return java.lang.String
   * @date 2021-09-13 09:43
   */
  public String login(LoginCmd loginQuery) {
    return loginFactory.handle(loginQuery.getLoginType())
        .login(loginQuery.getPrincipal(), loginQuery.getCredential(), operatorService.ip());
  }

  /**
   *  解析token获取用户ID
   *
   * @author ：qiaoliang
   * @param token : token
   * @return java.lang.Long
   * @date 2021-09-14 14:30
   */
  public Long checkToken(String token) {
    return securityService.decryptToken(token).getId();
  }

  /**
   *  获取在线用户
   *
   * @author ：qiaoliang
   * @return java.util.List<com.lanxiao.cloud.modules.oauth.application.dto.UserDetailsDto>
   * @date 2021-10-20 14:13
   */
  public ResultModel<UserDetailsDto> onLineUser(Long userId, PageQuery pageQuery) {
    List<UserDetails> all = securityService.onLineUser();
    if(Objects.nonNull(userId)) {
      all = all.stream().filter(a -> a.getId().equals(userId))
          .collect(Collectors.toList());
    }
    final List<UserDetails> userDetails = PageUtils.startPage(all, pageQuery.getPage(),
        pageQuery.getSize());
    return oauthAssembler.toUserDetailsDto(userDetails, all.size());
  }

  /**
   *  获取当前用户信息
   *
   * @author ：qiaoliang
   * @return com.lanxiao.cloud.modules.oauth.application.dto.LoginUserInfoDto
   * @date 2021-10-25 17:39
   */
  public LoginUserInfoDto loginUserInfoDto() {
    return oauthAssembler.toLoginUserInfoDto(securityService.onLineUser(operatorService.operator().toString()));
  }

}
